package com.youlai.system.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.common.model.Option;
import com.youlai.system.converter.DepartmentConverter;
import com.youlai.system.converter.ManagerConverter;
import com.youlai.system.mapper.DepartmentMapper;
import com.youlai.system.model.entity.Department;
import com.youlai.system.model.entity.Manage;
import com.youlai.system.model.form.DepartmentForm;
import com.youlai.system.model.vo.DepartmentVO;
import com.youlai.system.service.DepartmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

import static java.util.List.of;

@Service
@RequiredArgsConstructor
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    private final DepartmentConverter departmentConverter;
    private final DepartmentMapper departmentMapper;
    private final ManagerConverter managerConverter;
    @Override
    public List<DepartmentVO> listDepartments() {

//        List<Department> departments = this.list(
//                new QueryWrapper<Department>()
//                        .select()
//                        .leftJoin("employee", "department.department_id = employee.department_id")
//        );
//        return departmentConverter.entityVO(departments);

        List<DepartmentVO> list = departmentMapper.list();
        return list;
    }

    @Override
    public Integer saveDept(DepartmentForm formData) {
        Department entity = departmentConverter.formEntity(formData);

        boolean result = this.saveOrUpdate(entity);
        Assert.isTrue(result,"部门保存失败");
        return entity.getId();
    }

    @Override
    public boolean deleteByIds(String ids) {
        QueryWrapper<Department> departmentQueryWrapper = new QueryWrapper<Department>()
                .eq("id",ids)
                .or()
                .eq("pid",ids);


        return this.remove(departmentQueryWrapper);

    }

    @Override
    public List<Option> listManage() {
        List<Manage> manageList = departmentMapper.manageList();
        return managerConverter.entityListToOptionList(manageList);
    }

    @Override
    public DepartmentForm getDepartmentForm(Long id) {
        Department entity = this.getById(id);
        return departmentConverter.entityForm(entity);
    }

    @Override
    public String selectBydepId(Long deptId) {
        departmentMapper.selectNameById(deptId);
        return null;
    }

    @Override
    public Integer selectByDepName(String departmentName) {
        return departmentMapper.selectIdByName(departmentName);
    }


}
